package com.meme.editor.gui.level;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.JList;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionListener;

import org.apache.log4j.Logger;

import com.meme.editor.level.LevelFile;
import com.meme.editor.level.Actor;

import com.meme.editor.gui.EntityChildrenListView;
import com.meme.editor.gui.sprite2d.Sprite2dTreeView;

import java.util.List;
import java.util.LinkedList;

public class LevelTreeView extends JPanel 
	implements ListSelectionListener //,TreeSelectionListener, TreeExpansionListener, TreeModelListener
{
	static Logger logger = Logger.getLogger (LevelTreeView.class);

	
	LevelMainView mLevelMainView;
	EntityChildrenListView mEntityChildrenListView;
	
	JTree mTree;

	public LevelTreeView (LevelFile levelFile)
	{
		// temp
		mEntityChildrenListView = new EntityChildrenListView (levelFile.getActorSet(), false, true, true);
		mEntityChildrenListView.getListComponent().addListSelectionListener (this);
		
		setLayout (new BorderLayout ());
		add (mEntityChildrenListView, BorderLayout.CENTER);
	}
	
	public void setLevelMainView (LevelMainView levelMainView)
	{
		mLevelMainView = levelMainView;
	}
	
	boolean _fromLevelMainView = false;
	public void setListSelectedIndices (int[] indices)
	{
		_fromLevelMainView = true;
		mEntityChildrenListView.getListComponent().setSelectedIndices (indices);
		_fromLevelMainView = false;
	}
	
	public void valueChanged(javax.swing.event.ListSelectionEvent e)
	{
		if (mLevelMainView != null)
		{
			JList listControl = (JList) e.getSource();
			Object[] selectedObjects = listControl.getSelectedValues();
			
			List <Actor> actorList = new LinkedList <Actor> ();
			for (Object o : selectedObjects)
				actorList.add ((Actor)o);
			
			if (!_fromLevelMainView)
				mLevelMainView.synchronizedWithTreeView (actorList);
		}
	}
}
